package baseweb.springbootweb.controller;

import baseweb.springbootweb.pojo.Emp;
import baseweb.springbootweb.pojo.Pagebean;
import baseweb.springbootweb.pojo.Result;
import baseweb.springbootweb.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.List;

/**
 * 员工管理Controller
 */
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("/emps")
    public Result getpage(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize,
                          String name, Short gender,
                          @DateTimeFormat (pattern = "yyyy-MM-dd") LocalDate begin,
                          @DateTimeFormat (pattern = "yyyy-MM-dd") LocalDate end) {

        Pagebean pagebean=empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pagebean);
    }

    @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable List<Integer> ids) {

        empService.delete(ids);
        return Result.success();
    }


    @PostMapping("/emps")
    public Result save(@RequestBody Emp emp) {
        empService.save(emp);
        return Result.success();
    }


    @GetMapping("/emps/{id}")
    public Result getID(@PathVariable Integer id) {
        Emp emp=empService.getid(id);
        return Result.success(emp);
    }


    @PutMapping("/emps")
    public Result update(@RequestBody Emp emp) {
        empService.update(emp);
        return Result.success();
    }
}
